package GUI;
import Dominio.*;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JFrameClientes extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel1;
	private JScrollPane jScrollPane1;
	private JButton jButton1;
	private JTable jTable1;
	private JLabel jLabel1;
	private JPanel jContentPane = null;
	JFramePr papa=null;
	JTextField destino=null;
	JRadioButton radio1=null;
	private JButton jButton2;
	JRadioButton radio2=null;
	List<Cliente> lc=null;

	/**
	 * This is the default constructor
	 */
	public JFrameClientes(JFramePr p,JTextField d,JRadioButton r1,JRadioButton r2,List<Cliente>l) {
		super();
		papa=p;
		destino=d;
		radio1=r1;
		radio2=r2;
		lc=l;
		initialize();
	}
	


	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(575, 649);
		this.setContentPane(getJContentPane());
		this.setTitle("Lista Clientes");
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		Utilidades.CentrarJFrame(this);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	private JPanel getJPanel1() {
		if(jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(null);
			jPanel1.add(getJLabel1());
			jPanel1.add(getJScrollPane1());
			jPanel1.add(getJButton1());
			jPanel1.add(getJButton2());
		}
		return jPanel1;
	}
	
	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel("",JLabel.CENTER);
			jLabel1.setText("LISTA CLIENTES");
			jLabel1.setBounds(206, 23, 145, 18);
		}
		return jLabel1;
	}
	
	private JScrollPane getJScrollPane1() {
		if(jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(12, 61, 543, 459);
			jScrollPane1.setViewportView(getJTable1());
		}
		return jScrollPane1;
	}
	
	private JButton getJButton1() {
		if(jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Seleccionar");
			jButton1.setBounds(219, 560, 128, 26);
			jButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					jButton1ActionPerformed(evt);
				}
			});
		}
		return jButton1;
	}
	
	private JTable getJTable1() {
		String [][]m=Utilidades.construirMatrizTablaClientes(lc);
		if(jTable1 == null) {
			TableModel jTable1Model = 
				new DefaultTableModel(
						m,
						new String[] { "DNI", "Nombre","Apellidos" });
			jTable1 = new JTable();
			jTable1.setModel(jTable1Model);
		}
		return jTable1;
	}
	
	private void jButton1ActionPerformed(ActionEvent evt) {
		System.out.println("jButton1.actionPerformed, event="+evt);
		int fila=jTable1.getSelectedRow();
		int dni=Integer.parseInt(jTable1.getValueAt(fila, 0).toString());
		Cliente c =Cliente.consultarCliente(dni);
		if(radio2!=null){
			if(c.getEsSocio()==1){
				radio2.setEnabled(true);
			}
			else{
				radio2.setEnabled(false);
				radio1.setSelected(true);
			}
		}
		destino.setText(String.valueOf(dni));
		papa.setEnabled(true);
		this.dispose();
	}
	
	private JButton getJButton2() {
		if(jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Cancelar");
			jButton2.setBounds(397, 560, 128, 26);
			jButton2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					jButton2ActionPerformed(evt);
				}
			});
		}
		return jButton2;
	}
	
	private void jButton2ActionPerformed(ActionEvent evt) {
		System.out.println("jButton2.actionPerformed, event="+evt);
		papa.setEnabled(true);
		this.dispose();
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
